﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Ninject;
using CslaHispano.EncapsulatedInvoke.Domain.Repository;

namespace CslaHispano.EncapsulatedInvoke.Domain.Entities
{
    public partial class SystemEdit
    {
        #region Methods

        private void LoadChilds()
        {            
            Modules = DataPortal.FetchChild<SystemModules>(SystemId);
        }

        private void UpdateChilds()
        {            
            DataPortal.UpdateChild(Modules, this);
        }

        private void CreateChilds()
        {            
            Modules = DataPortal.CreateChild<SystemModules>();
        }

        #endregion

        #region Data Portal

        [RunLocal()]
        protected override void DataPortal_Create()
        {
            using (BypassPropertyChecks)
            {
                SystemId  = Guid.NewGuid().ToString();
                Status = true;                
            }
            CreateChilds();
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(SingleCriteria<SystemEdit, string> criteria)
        {
            var dal = _NInject.Get<ISystemEditRepository>();

            using (var data = dal.Fetch(criteria.Value))
            {
                while (data.Read())
                {
                    using (BypassPropertyChecks)
                    {
                        SystemId = data.GetString(data.GetOrdinal("SystemId"));
                        Tag = data.GetString(data.GetOrdinal("Tag"));
                        Code = data.GetString(data.GetOrdinal("Code"));
                        Description = data.GetString(data.GetOrdinal("Description"));
                        Path = data.GetString(data.GetOrdinal("Path"));
                        Status = data.GetBoolean(data.GetOrdinal("Status"));
                    }
                }
                LoadChilds();
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            //var dal = IoCFactory.Instance.CurrentContainer.Resolve<ISystemRepository>();

            //using (BypassPropertyChecks)
            //{
            //    dal.Insert(SystemId , Tag, Code, Description, Path, Status);
            //}
            //UpdateChilds();
            //dal = null;
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            //var dal = IoCFactory.Instance.CurrentContainer.Resolve<ISystemRepository>();

            //using (BypassPropertyChecks)
            //{                
            //    dal.Update(SystemId, Tag, Code, Description, Path, Status);
            //}
            //FieldManager.UpdateChildren(this);
            //dal = null;
        }

        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new SingleCriteria<SystemEdit, string>(ReadProperty<string>(SystemIdProperty)));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(SingleCriteria<SystemEdit, string> criteria)
        {
            //var dal = IoCFactory.Instance.CurrentContainer.Resolve<ISystemRepository>();

            //using (BypassPropertyChecks)
            //{
            //    dal.Delete(criteria.Value);
            //}
            
            //CreateChilds();
            //dal = null;
        }

        #endregion
    }
}
